//
//  GroupGongGaoViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/9.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GroupGongGaoViewController.h"
#import "GroupGongGaoModel.h"
#import "GroupGongGaoTableViewCell.h"
#import "AddGongGaoViewController.h"
#import "GongGaoDetailViewController.h"


@interface GroupGongGaoViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *groupGongGaoTableview;
@property (nonatomic, strong) NSMutableArray *groupGongGaoDataArray; // 数据源数组

@end

@implementation GroupGongGaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 构建数据源
    [self addData];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"小组公告";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightImageView.image = [UIImage imageNamed:@"add-myGroup"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageView];
    // 添加手势
    UITapGestureRecognizer *rightTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightGes:)];
    [rightImageView addGestureRecognizer:rightTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加公告
- (void)rightGes:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"添加公告");
    AddGongGaoViewController *addGongGaoVC = [[AddGongGaoViewController alloc] init];
    [self.navigationController pushViewController:addGongGaoVC animated:YES];
}

#pragma mark - 构建数据源
- (void)addData
{
    // 构造模拟数据
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"2015年10.27", @"_time", @"张国文", @"_senderName", @"公告标题1" , @"_gongGaoTitle", @"暗示对奇偶if及偶尔去忘记哦大家爱哦是基地哦,四喜那个字典", @"_content" , nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2016年1.27", @"_time", @"带秋天", @"_senderName",@"公告标题2" , @"_gongGaoTitle", @"暗示对奇偶if及偶尔,去忘记哦大家爱哦是基地哦,四喜那个字典", @"_content" , nil];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"2016年3.15", @"_time", @"杨幂", @"_senderName", @"公告标题3" , @"_gongGaoTitle", @"暗示对奇偶if及偶尔去忘记哦大,家爱哦是基地哦,四喜那个字典,你好阿斯蒂芬而且为人,ICV型敬爱个囧", @"_content" , nil];
    NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"2016年3.15", @"_time", @"周杰伦", @"_senderName", @"公告标题4" , @"_gongGaoTitle", @"钻铣床对奇偶if及偶尔去忘记哦大,家爱哦是基地哦,四喜那个字典,你好阿斯蒂芬而且为人,ICV型敬爱个囧", @"_content" , nil];
    NSArray *dataArray = [NSArray arrayWithObjects:dic1, dic2, dic3, dic4, nil];
    
    // 初始化数据源数组
    self.groupGongGaoDataArray = [NSMutableArray array];
    for (NSDictionary *tempDic in dataArray) {
        GroupGongGaoModel *model = [[GroupGongGaoModel alloc] init];
        [model setValuesForKeysWithDictionary:tempDic];
        [self.groupGongGaoDataArray addObject:model];
    }
    // 将,变成,\n
    for (GroupGongGaoModel *tempModel in self.groupGongGaoDataArray) {
        NSString *tempString = tempModel.content;
       NSString *tempString1 = [tempString stringByReplacingOccurrencesOfString:@"," withString:@",\n"];
        tempModel.content = tempString1;
        NSLog(@"tempString1 = %@", tempString1);
    }
    NSLog(@"%@", self.groupGongGaoDataArray);
    
}

#pragma mark - 布局
- (void)addMySubview
{
    self.groupGongGaoTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight  -  64) style:UITableViewStylePlain];
    self.groupGongGaoTableview.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    self.groupGongGaoTableview.separatorColor = [UIColor clearColor];
    self.groupGongGaoTableview.bounces = NO;
    self.groupGongGaoTableview.dataSource = self;
    self.groupGongGaoTableview.delegate = self;
    [self.view addSubview:_groupGongGaoTableview];
}

#pragma mark - tableview代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return self.groupGongGaoDataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"groupGongGao";
    GroupGongGaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[GroupGongGaoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 取得model
    cell.model = self.groupGongGaoDataArray[indexPath.section];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
    backGroundView.backgroundColor = [UIColor clearColor];
    return backGroundView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20 * kAdjustParamater;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    GroupGongGaoModel *model = self.groupGongGaoDataArray[indexPath.section];
//    NSString *tempString = model.content;
//    CGFloat height = [[NetStatusHandle shareHandle] adjustHeight:tempString size:15 width:305];
//    return height + 95 * kAdjustParamater + 23  * kAdjustParamater;
    GroupGongGaoTableViewCell *cell = (GroupGongGaoTableViewCell *)[self tableView:self.groupGongGaoTableview cellForRowAtIndexPath:indexPath];
    return cell.myContentLabel.bottom + 23 * kAdjustParamater;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GroupGongGaoModel *model = self.groupGongGaoDataArray[indexPath.section];
    NSLog(@"发送者姓名 = %@", model.senderName);
    GongGaoDetailViewController *gongGaoDetailVC = [[GongGaoDetailViewController alloc] init];
    gongGaoDetailVC.myModel = model;
    [self.navigationController pushViewController:gongGaoDetailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
